package aod.android.com.bernay.live.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import aod.android.com.bernay.R;
import aod.android.com.bernay.live.bean.HomeListBean;
import chen.lion.hilib.image.ImageLoader;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * <p>文件描述：<p>
 * <p>作者：${SYC}<p>
 * <p>创建时间：2019/4/4<p>
 */
public class LiveAdapter extends BaseAdapter {
    private List<HomeListBean.DataBean> inviteBeanList;
    private Context mContext;

    public LiveAdapter(Context mContext, List<HomeListBean.DataBean> inviteBeanList) {
        this.inviteBeanList = inviteBeanList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return inviteBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return inviteBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

//    public void setOnItemClickListener(ClientInviteHistoryAdapter.OnItemClickListener listener) {
//        this.listener = listener;
//    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.live_ietm_lay, null, false);
            viewHolder.live_video_vv = convertView.findViewById(R.id.live_video_vv);
            viewHolder.live_video_title = convertView.findViewById(R.id.live_video_title);
            viewHolder.live_video_time = convertView.findViewById(R.id.live_video_time);
            viewHolder.live_userhead_iv = convertView.findViewById(R.id.live_userhead_iv);
            viewHolder.live_user_name_tv = convertView.findViewById(R.id.live_user_name_tv);
            viewHolder.live_timeadd_address_tv = convertView.findViewById(R.id.live_timeadd_address_tv);
            viewHolder.live_like_nums_tv = convertView.findViewById(R.id.live_like_nums_tv);
            viewHolder.live_comments_nums_tv = convertView.findViewById(R.id.live_comments_nums_tv);
            viewHolder.live_video_ray = convertView.findViewById(R.id.live_video_ray);
            viewHolder.live_img_one = convertView.findViewById(R.id.live_img_one);
            viewHolder.live_img_two = convertView.findViewById(R.id.live_img_two);
            viewHolder.live_img_three = convertView.findViewById(R.id.live_img_three);
            viewHolder.live_img_title = convertView.findViewById(R.id.live_img_title);
            viewHolder.live_img_lay = convertView.findViewById(R.id.live_img_lay);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        HomeListBean.DataBean bean = inviteBeanList.get(position);
        if (bean.getType().equals("2")) {
            viewHolder.live_img_lay.setVisibility(View.GONE);
            viewHolder.live_video_ray.setVisibility(View.VISIBLE);
            if(bean.getSource()!=null && !bean.getSource().equals("")){
                viewHolder.live_video_vv.setUp(bean.getSource(), "http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640", bean.getTitle());

            }
        }   else {
            viewHolder.live_img_lay.setVisibility(View.VISIBLE);
            viewHolder.live_video_ray.setVisibility(View.GONE);
            if(bean.getSourceStr()!=null && !bean.getSourceStr().equals("")){
                List<String> list =  new ArrayList<>();
                List<String> idList = Arrays.asList(bean.getSourceStr().split(","));//根据逗号分隔转化为list
                ImageLoader.with(mContext).load("http://ppzlq412k.bkt.clouddn.com/"+idList.get(0),viewHolder.live_img_one);
                ImageLoader.with(mContext).load("http://ppzlq412k.bkt.clouddn.com/"+idList.get(1),viewHolder.live_img_two);
                ImageLoader.with(mContext).load("http://ppzlq412k.bkt.clouddn.com/"+idList.get(2),viewHolder.live_img_three);
            }
        }
       // viewHolder.live_video_vv.setVideoPath(bean.getVideoPath());
        viewHolder.live_video_title.setVisibility(View.GONE);
        viewHolder.live_img_title.setText(bean.getTitle());
        viewHolder.live_video_time.setText("00:26");
        ImageLoader.with(mContext).load(bean.getHeadPicUrl(),viewHolder.live_userhead_iv);
        viewHolder.live_user_name_tv.setText(bean.getNickName());

        viewHolder.live_timeadd_address_tv.setText("一天前"+"."+"北京");
        viewHolder.live_like_nums_tv.setText(bean.getGiveNum()+"");
        viewHolder.live_comments_nums_tv.setText(bean.getCommentNum()+"");


        return convertView;
    }


    class ViewHolder {
        private JCVideoPlayer live_video_vv;//播放器
        private TextView live_video_title;//视频直播标题
        private TextView live_video_time;//播放时间
        private ImageView live_userhead_iv;//主播头像
        private TextView live_user_name_tv;//主播昵称
        private TextView live_timeadd_address_tv;//直播的开始时间和地点
        private TextView live_like_nums_tv;//直播点赞数
        private TextView live_comments_nums_tv;//评论数
        private RelativeLayout live_video_ray;//视频直播布局
        private LinearLayout live_img_lay;//图片直播布局
        private ImageView live_img_one;//图片直播第一张图片
        private ImageView live_img_two;//图片直播第一张图片
        private ImageView live_img_three;//图片直播第一张图片
        private TextView live_img_title;//图片直播标题

    }
}
